package com.istudy.service.imp;

import com.istudy.dao.BaseDao;
import com.istudy.dao.impl.UploadDaoImpl;
import com.istudy.entity.Upload;
import com.istudy.service.interfaces.UploadService;
import com.istudy.utils.Page;
import org.omg.CORBA.Request;

import javax.servlet.http.HttpServletRequest;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 视频service接口实现类
 * @date 2020-09-01
 * @author GQ
 */
public class UploadServiceImpl implements UploadService {
    UploadDaoImpl uploadDao = new UploadDaoImpl();

    /**
     * 添加上传记录
     * @param upload 实体类
     * @return 受影响行数
     */
    @Override
    public boolean addUpload(Upload upload) {
        boolean b = false;
        Connection connection = null;
        connection = BaseDao.getConnection();
        try {
            int i = uploadDao.insertUpload(upload);
            b = i > 0;
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return b;
    }
    /**
     * 删除上传记录
     * @param id id
     * @return 受影响行数
     */
    @Override
    public boolean deleteUploadById(int id) {
        boolean b = false;
        Connection connection = null;
        connection = BaseDao.getConnection();
        try {
            int i = uploadDao.deleteUpload(id);
            b = i > 0;
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return b;
    }
    /**
     * 获取展示列表
     * @return list
     */
    @Override
    public List<Upload> getAllList(HttpServletRequest request, int num) {
        Connection connection = null;
        Page page=new Page();
        connection = BaseDao.getConnection();
        List<Upload> list;
        page.setPageSize(4);
        try {
            page.setCount(uploadDao.getCount());
            if (num!=0){
                page.setPageNo(num);
            }
            list = uploadDao.getList(page);
            request.setAttribute("page",page);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    /**
     * 获取upload
     *
     * @param request
     * @param num
     * @return upload
     */
    @Override
    public Upload getUpload(HttpServletRequest request, int num) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        Upload upload;
        try {

            upload = uploadDao.getUoload(num);

        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return upload;

    }
}
